import api from '@/api';

export default {
  namespace: 'buyRule',

  state: {
    buyRules: {
      list: [],
      pagination: {},
    },
  },

  effects: {
    *fetch({ payload }, { call, put }) {
      let res = yield call(api.buyRule.query, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveBuyRules',
          payload: {
            list: res.data,
            pagination: res.pagination,
          },
        });
      }
    },
    *fetchRuleById({ payload }, { call, put }) {
      return yield call(api.buyRule.queryRuleById, payload);
    },
    *create({ payload }, { call }) {
      debugger
      return yield call(api.buyRule.create, payload)
    },
    *audit({ payload }, { call }) {
      return yield call(api.buyRule.audit, payload)
    },
    *delete({ payload }, { call }) {
      return yield call(api.buyRule.delete, payload)
    },
    *enable({ payload }, { call }) {
      return yield call(api.buyRule.enable, payload)
    },
    *disable({ payload }, { call }) {
      return yield call(api.buyRule.disable, payload)
    },
  },

  reducers: {
    saveBuyRules(state, { payload }) {
      return {
        ...state,
        buyRules: payload,
      };
    },
  },
};
